package com.example.myhttplibrary.base;

import io.reactivex.Observer;
import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.disposables.Disposable;

public abstract   class BaseCacllk<T> implements Observer<BaseData<T>> {

    private CompositeDisposable compositeDisposable=new CompositeDisposable();
    @Override
    public void onSubscribe(Disposable d) {
            compositeDisposable.add(d);
    }

    @Override
    public void onNext(BaseData<T> tBaseData) {
        if (tBaseData.getErrorCode()==0){
            onSuccess(tBaseData.getData());
        }else {
            onFailed(tBaseData.getErrorMsg(),tBaseData.getErrorCode());
        }
    }

    @Override
    public void onError(Throwable e) {
        compositeDisposable.clear();
    }

    @Override
    public void onComplete() {
        compositeDisposable.clear();
    }

    abstract void onSuccess(T t);
    abstract void onFailed(String error,int code);

}
